package com.yycx.module.bus.provider.handler;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.interceptor.GetInterceptor;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.admin.client.entity.BaseUser;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.BusSaleman;
import com.yycx.module.bus.provider.service.BusPaymentService;
import com.yycx.module.file.provider.service.SysFileService;
import com.yycx.module.system.client.entity.SysBank;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 订单详情信息
 *
 * @author zyf
 */
@Component("orderGetHandler")
public class OrderGetHandler implements GetInterceptor {

    @Autowired
    private TaskService taskService;

    @Autowired
    private SysFileService fileService;


    @Autowired
    private BusPaymentService paymentService;


    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, EntityMap entityMap) {
        cq.select(BusDept.class, "deptName");
        cq.addSelect(BusSaleman.class, "saleManName","linkTel");
        cq.addSelect(BaseUser.class, "nickName");
        cq.addSelect(SysBank.class, "bankName");
        cq.createJoin(BusDept.class);
        cq.createJoin(BaseUser.class);
        cq.createJoin(SysBank.class);
        cq.createJoin(BusSaleman.class);
    }

    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Integer orderState=map.getInt("orderState");
        //是否允许核算
        Boolean calculate = OpenHelper.hasRole("ROLE_ZJL","ROLE_ZJL_LD","ROLE_KJ","ROLE_NQBZ","ROLE_CWBZ","ROLE_KJ_LD","ROLE_NQBZ_LD","ROLE_CWBZ_LD","ROLE_CW_JSBX");
        if(orderState.equals(2)){
            calculate = OpenHelper.hasRole("ROLE_ZJL","ROLE_ZJL_LD","ROLE_CWBZ","ROLE_CWBZ_LD","ROLE_NQBZ_LD","ROLE_KJ_LD","ROLE_CGBZ_LD","ROLE_CW_JSBX");
        }
        map.put("calculate", calculate);
        Long orderId = map.getLong("orderId");
        Long orderDetailsId = cq.getLong("orderDetailsId");
        if(FlymeUtils.isNotEmpty(orderId)){
            //查询已结算明细
           List<EntityMap> paymentList= paymentService.getByOrderId(orderId);
           map.put("paymentList", paymentList);
        }

        Integer customBondType=map.get("customBondType");
        if(customBondType.equals(0)){
           map.put("bankName","客户付");
        }
        String processInstanceId = map.get("processInstanceId");
        if (FlymeUtils.isNotEmpty(processInstanceId)) {
            //查询当前审批节点
            Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).active().singleResult();
            if (FlymeUtils.isNotEmpty(task)) {
                map.put("taskName", task.getName());
                map.put("taskId", task.getId());
                map.put("taskDefinitionKey", task.getTaskDefinitionKey());
            }
        }

    }
}
